package com.zwh.chatgpt.test.test;

import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;


//连接豆包api测试
public class doubao {
    public static void main(String[] args) throws IOException, InterruptedException {

            // 创建HttpClient实例
            HttpClient client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_1_1)
                    .connectTimeout(Duration.ofSeconds(10))
                    .build();

            // 请求URL
            String apiUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";

            // 请求体JSON
            String paramJson = """
                {
                    "model": "doubao-1-5-thinking-pro-250415",
                    "messages": [
                        {"role": "system", "content": "你要回答得文雅一些"},
                        {"role": "user", "content": "李白是什么朝代的"}
                    ]
                }
                """;//sys是相当于对后台系统的暗示，user的就是表面要回答的问题

            // 构建HTTP请求
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(apiUrl))
                    .timeout(Duration.ofSeconds(30))
                    .header("Content-Type", "application/json")
                    .header("Authorization", "Bearer c9f90c74-5a24-4f1c-9908-0e375e830622")
                    .POST(HttpRequest.BodyPublishers.ofString(paramJson))
                    .build();

            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 处理响应
            System.out.println("状态码: " + response.statusCode());
            System.out.println("响应体: " + response.body());

        }

}